package cn.iocoder.yudao.module.biz.controller.admin.risk.controlMeasures;

import cn.iocoder.yudao.module.biz.controller.admin.risk.controlMeasures.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.controlMeasures.BizControlMeasuresConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.controlMeasures.BizControlMeasuresDO;
import cn.iocoder.yudao.module.biz.service.risk.controlMeasures.BizControlMeasuresService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 管控措施")
@RestController
@RequestMapping("/biz/control-measures")
@Validated
public class BizControlMeasuresController {

    @Resource
    private BizControlMeasuresService controlMeasuresService;

    @PostMapping("/create")
    @ApiOperation("创建管控措施")
    @PreAuthorize("@ss.hasPermission('biz:control-measures:create')")
    public CommonResult<String> createControlMeasures(@Valid @RequestBody BizControlMeasuresCreateReqVO createReqVO) {
        return success(controlMeasuresService.createControlMeasures(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新管控措施")
    @PreAuthorize("@ss.hasPermission('biz:control-measures:update')")
    public CommonResult<Boolean> updateControlMeasures(@Valid @RequestBody BizControlMeasuresUpdateReqVO updateReqVO) {
        controlMeasuresService.updateControlMeasures(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除管控措施")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:control-measures:delete')")
    public CommonResult<Boolean> deleteControlMeasures(@RequestParam("id") String id) {
        controlMeasuresService.deleteControlMeasures(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得管控措施")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:control-measures:query')")
    public CommonResult<BizControlMeasuresRespVO> getControlMeasures(@RequestParam("id") String id) {
        BizControlMeasuresDO controlMeasures = controlMeasuresService.getControlMeasures(id);
        return success(BizControlMeasuresConvert.INSTANCE.convert(controlMeasures));
    }

    @GetMapping("/list")
    @ApiOperation("获得管控措施列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:control-measures:query')")
    public CommonResult<List<BizControlMeasuresRespVO>> getControlMeasuresList(@RequestParam("ids") Collection<String> ids) {
        List<BizControlMeasuresDO> list = controlMeasuresService.getControlMeasuresList(ids);
        return success(BizControlMeasuresConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得管控措施分页")
    @PreAuthorize("@ss.hasPermission('biz:control-measures:query')")
    public CommonResult<PageResult<BizControlMeasuresRespVO>> getControlMeasuresPage(@Valid BizControlMeasuresPageReqVO pageVO) {
        PageResult<BizControlMeasuresDO> pageResult = controlMeasuresService.getControlMeasuresPage(pageVO);
        return success(BizControlMeasuresConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出管控措施 Excel")
    @PreAuthorize("@ss.hasPermission('biz:control-measures:export')")
    @OperateLog(type = EXPORT)
    public void exportControlMeasuresExcel(@Valid BizControlMeasuresExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizControlMeasuresDO> list = controlMeasuresService.getControlMeasuresList(exportReqVO);
        // 导出 Excel
        List<BizControlMeasuresExcelVO> datas = BizControlMeasuresConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "管控措施.xls", "数据", BizControlMeasuresExcelVO.class, datas);
    }

}
